package com.ruoyi.project.ins.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.project.ins.domain.InsMessage;
import com.ruoyi.project.ins.domain.InsSendMessage;
import com.ruoyi.project.ins.service.IInsMessageService;
import com.ruoyi.project.mapper.InsMessageMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

@Service
@Slf4j
public class InsMessageServiceImpl implements IInsMessageService {

    @Autowired
    private InsMessageMapper insMessageMapper;

    @Autowired
    protected Validator validator;

    @Override
    public List<InsMessage> selectInsMessageList(InsMessage insMessage) {
        return insMessageMapper.selectInsMessageList(insMessage);
    }

    @Override
    public int insertInsMessage(InsMessage insMessage) {
        return insMessageMapper.insertInsMessage(insMessage);
    }

    @Override
    public int updateInsMessage(InsMessage insMessage) {
        return insMessageMapper.updateInsMessage(insMessage);
    }

    @Override
    public InsMessage selectAccountById(Long id) {
        return insMessageMapper.selectAccountById(id);
    }

    @Override
    public int deleteByIds(Long[] deleteIds) {
        return insMessageMapper.deleteByIds(deleteIds);
    }

    @Override
    public List<InsMessage> selectByIds(Long[] ids) {
        return insMessageMapper.selectByIds(ids);
    }

    @Override
    public int batchInsertInsMessage(List<InsMessage> insMessageList) {
        return insMessageMapper.batchInsertInsMessage(insMessageList);
    }

    @Override
    public List<InsSendMessage> selectNoSentMessage() {
        return insMessageMapper.selectNoSentMessage();
    }

    @Override
    public void batchUpdateMessages(List<InsMessage> messageList) {
        if (CollectionUtils.isEmpty(messageList)) {
            return;
        }
        int total = messageList.size();
        for (int i = 0; i < total; i += Constants.MYBATIS_BATCH_MAX_SIZE) {
            int end = Math.min(i + Constants.MYBATIS_BATCH_MAX_SIZE, total);
            List<InsMessage> subList = messageList.subList(i, end);
            insMessageMapper.batchUpdateMessages(subList);  // 调用MyBatis批量更新
        }
    }
}
